import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ShopCarView extends JFrame {

    public ShopCarView(){

        JLabel l_title=new JLabel("我的购物车");
        JLabel l_goods=new JLabel("商品名称");
        JTextField goodsName=new JTextField(10);
        //创建表格对象，要求对象名为jTable
        JTable jTable=new JTable();
        DefaultTableModel tableModel = new DefaultTableModel();
        jTable.setModel(tableModel);
        tableModel.addColumn("商品名称");
        tableModel.addColumn("数量");
        Object[] rowData={"鼠标",1};
        tableModel.addRow(rowData);
        JButton add=new JButton("加入购物车");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               //添加商品
                String gname =goodsName.getText().trim();
                if(!gname.isEmpty()){
                    int row=jTable.getRowCount();
                    Boolean a=true;
                    for(int i=0;i<row;i++){
                        if(jTable.getValueAt(i,0).toString().equals(gname)){
                            int num=(Integer) jTable.getValueAt(i,1);
                            jTable.setValueAt(num+1,i,1);
                            a=false;
                        }
                    }
                    if (a){
                        Object[] rowData={gname,1};
                        tableModel.addRow(rowData);
                    }
                    goodsName.setText("");
                }
            }
        });

        //创建删除按钮，对象名为del，并给按钮添加功能
        JButton del=new JButton("删除");
        del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row=jTable.getRowCount();
                int row1=jTable.getSelectedRow();
                if(row>0){
                    tableModel.removeRow(row1);
                }else {
                    JOptionPane.showMessageDialog(null,"购物车为空");
                }
            }
        });
        JPanel jPanel=new JPanel();
        jPanel.setLayout(null);
        l_title.setBounds(130,20,80,20);
        jPanel.add(l_title);
        l_goods.setBounds(50,40,60,20);
        jPanel.add(l_goods);
        goodsName.setBounds(120,40,100,20);
        jPanel.add(goodsName);
        add.setBounds(230,40,100,20);
        jPanel.add(add);
        JScrollPane jScrollPane = new JScrollPane(jTable);
        jScrollPane.setBounds(10,60,370,150);
        jPanel.add(jScrollPane);
        del.setBounds(120,220,60,20);
        jPanel.add(del);
        this.add(jPanel);
        this.setTitle("购物车页面");
        this.setSize(400,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ShopCarView();
    }
}
